// NightShader.java - 基于BaseShader创建夜间效果
package com.goldsprite.gameframeworks.shaders;

import com.goldsprite.gameframeworks.log.Debug;
import com.goldsprite.gameframeworks.utils.TimeUtils;

public class NightShader extends BaseShader {
    private int uTimeLocation;
    private int uDayTimeLocation;

	private float realTime;
	private float dayTimeSeconds = 3f * 60;//3分钟一个昼夜

    public NightShader() {
        super("shaders/night.vert", "shaders/night.frag");
    }

    @Override
    protected void initUniforms() {
		uTimeLocation = shaderProgram.getUniformLocation("u_time");
		uDayTimeLocation = shaderProgram.getUniformLocation("u_dayTime");
    }

    @Override
    public void setUniforms() {
        setUniformf(uTimeLocation, getUTime());
        setUniformf(uDayTimeLocation, dayTimeSeconds);
    }

	@Override
	public void update(float delta) {
		super.update(delta);

		realTime = (getUTime() * 24.0f + 6 * dayTimeSeconds) / 24.0f;//将开始时间偏移到正午12点, 因为为0时shader最亮

		float curDays = realTime / dayTimeSeconds;
		float curTime = curDays % dayTimeSeconds;
		int curIntDay = (int)(curDays - curTime);
		long dateTimeNanos = (long)(curDays * TimeUtils.Day2Nanos);
		long dateNanos = (long)(curIntDay * TimeUtils.Day2Nanos);
		long timeNanos = (long)(curTime * TimeUtils.Day2Nanos);
		String curFormatDateTime = TimeUtils.formatRelativeTime(dateTimeNanos, "yyyy年MM月dd日 HH时mm分ss秒");
		String curFormatDate = TimeUtils.formatRelativeTime(dateNanos, "yyyy年MM月dd日");
		String curFormatTime = TimeUtils.formatRelativeTime(timeNanos, "HH时mm分ss秒");
		Debug.infoT("Shader", "当前天数: %s", curIntDay);
		Debug.infoT("Shader", "游戏全日期: %s", curFormatDateTime);
		Debug.infoT("Shader", "游戏日期: %s", curFormatDate);
		Debug.infoT("Shader", "时间(24H): %s", curFormatTime);
	}

	public void setDayTimeMinutes(float dayTimeMinutes) {
		setDayTimeSeconds(dayTimeMinutes * 60);
	}
	public void setDayTimeSeconds(float dayTimeSeconds) {
		this.dayTimeSeconds = dayTimeSeconds;
	}
}
